package com.zhx.mybatislog.handler;

import com.alibaba.fastjson.JSON;
import com.zhx.mybatislog.bo.CompareResult;
import com.zhx.mybatislog.bo.EditBo;
import com.zhx.mybatislog.bo.OptBo;
import com.zhx.mybatislog.user.dao.ModifyRecordMapper;
import com.zhx.mybatislog.user.entity.ModifyRecord;
import com.zhx.mybatislog.utils.LogContext;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @Description TODO
 *
 */
@Service
public class DataLogDeal implements DataLogHandler {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
  @Autowired
  ModifyRecordMapper modifyRecordMapper;
	@SneakyThrows
	public void insertHandler(InsertInfo insertInfo) {
		StringBuilder sb = new StringBuilder();
		sb.append("插入");
		sb.append(insertInfo.getBasicInfo().getTbName());
		sb.append("表");
		OptBo opt = new OptBo();
		opt.setData(insertInfo.getInsertObj());
		opt.setDesc(sb.toString());
		logger.debug(sb.toString());
	}

	public void updateHandler(UpdateInfo updateInfo) {
		List<EditBo> cr = null;
		try {
			cr = updateInfo.getCompareResult();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			//Todo
			logger.info("");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("更新");
		sb.append(updateInfo.getBasicInfo().getTbName());
		sb.append("表");
		sb.append(cr.size() + "条数据");
		OptBo opt = new OptBo();
		List<List<String>> updateer = new ArrayList<List<String>>();
		for (int i = 0; i < cr.size(); i++) {
			List<String> no1 = new ArrayList<String>();
			EditBo editBo = cr.get(i);
			List<CompareResult> e = editBo.getModifyColumns();
			no1.clear();
			StringBuilder modifyContent = new StringBuilder("ModifyContent:");
			for(Iterator<CompareResult> it = e.iterator();it.hasNext();){
				//Todo CompareResult这个类可以换成ModifyRecord
				CompareResult r = it.next();
				modifyContent.append("把《" + r.getFieldComment() + "》从<" + r.getOldValue() + ">改成<" + r.getNewValue() + ">");
				if(it.hasNext())
					modifyContent.append(",");

                ModifyRecord modifyRecord=new ModifyRecord().setColumnName(r.getFieldName())
                        .setModifyAfter(r.getNewValue().toString() )
                        .setModifyBefore(r.getOldValue() .toString())
                        .setModifyBy(18006)
                        .setModifyType("变更"+r.getFieldComment() )
						.setTableName(updateInfo.getBasicInfo().getTbName());
                try{
                    modifyRecordMapper.saveModifyRecord(modifyRecord);
                }catch(Exception e1){
                    e1.printStackTrace();
                    new RuntimeException("新增报错");
                }
			}

			no1.add(modifyContent.toString());
			Map<String,Object> primaryKeys = editBo.getPrimaryKeys();
			no1.add("PrimaryKeys:"+ JSON.toJSONString(primaryKeys));
			logger.info( no1.toString());
			updateer.add(no1);
		}
		opt.setData(updateer);
		opt.setDesc(sb.toString());
//		LogContext.optLog().getOpts().add(opt);
		logger.info( sb.toString());
	}
	@SneakyThrows
	public void deleteHandler(DeleteInfo deleteInfo) {
		StringBuilder sb = new StringBuilder();
		sb.append("删除");
		sb.append(deleteInfo.getBasicInfo().getTbName());
		sb.append("表");
		OptBo opt = new OptBo();
		opt.setData(deleteInfo.getDeleteObj());
		opt.setDesc(sb.toString());
		LogContext.optLog().getOpts().add(opt);
		logger.debug("          " + sb.toString());
	}
}